import { RouteRecordRaw, RouteRecordRedirectOption } from 'vue-router'; 
import router from '@/project/crmManage/router';
import type { MenuInfo, MenuTreeType } from '@/domain/authority/types/menu';

export type RouterType = RouteRecordRaw & { path?: string; chname?: string; routerType?: 1 | 2 | 3; children?: any; redirect?: RouteRecordRedirectOption };

export type TreeNodeData<T = { id: string; pid: string; child: TreeNodeData[] }> = {
  [c: string]: string | T[];
};

export function routerRecord(nodeList: Array<RouterType>, fullRouter: RouteRecordRaw[]) {
  if (!nodeList || nodeList.length == 0) {
    return null;
  }
  nodeList.forEach((node) => {
    fullRouter.push(node);
    if (node.children) {
      routerRecord(node.children, fullRouter);
    }
  });
}

export function getRouterRecord(chirdRoutes: RouteRecordRaw[]) {
  const fullRouter: RouteRecordRaw[] = [];
  routerRecord(chirdRoutes, fullRouter);
  return fullRouter;
}

/**
 *
 * @param nodeList 菜单节点
 * @param pid 父id
 * @returns
 */
export function listToRouterTree(nodeList: MenuInfo[], pid: number | string): Array<RouterType> {
  const rootNode = nodeList.filter((node) => node.parent_id === pid);

  if (rootNode.length === 0) {
    return [];
  }
  const node = rootNode.map((val) => {
    return {
      path: val.router,
      routerType: val.router_type,
      // component: val.router_type == 1 ? import('../pages' + val.path) : undefined,
      chname: val.menu_name,
      name: val.menu_name,
      children: listToRouterTree(nodeList, val.menu_id)
    };
  });
  return node as Array<RouterType>;
}

/**
 *
 * @param nodeList 菜单树形编辑节点
 * @param pid 父id
 * @param disabledTag 是否依照参数设置disable属性
 * @returns
 */
export function listToTree(nodeList: MenuInfo[], pid: number | string, disabledTag = false): MenuTreeType {
  const rootNode = nodeList.filter((node) => node.parent_id === pid);

  if (rootNode.length === 0) {
    return [];
  }

  const node = rootNode.map((val) => {
    const dtag = disabledTag ? (val.status === 1 ? false : true) : disabledTag;
    return { ...val, key: val.menu_id, disabled: dtag, title: val.menu_name, children: listToTree(nodeList, val.menu_id) };
  });

  return node;
}

export function getStandardTree(node: TreeNodeData, key: string, pid: string, child: string): any {
  if (!node) {
    return [];
  }
  const list = (node[child] as unknown as TreeNodeData[]).map((value) => {
    const children = getStandardTree(value, key, pid, child);
    return children;
  });
  const n = { id: node[key], pid: node[pid], child: list };
  return n;
}

/**设置router */
export const setRouter = (menuList: MenuInfo[]) => {
  menuList
    .filter((val) => val.parent_id != 0)
    .map((val) => {
      if (val.router != '' && val.path != '') { 
          router.addRoute('home', {
            path: val.router,
            routerType: val.router_type,
            component: undefined!,
            chname: val.menu_name,
            name: val.menu_name
          } as RouteRecordRaw);
       
      }
    });
};
